package com.lry;

import com.lry.task.Task;
import com.lry.task.TaskStrategy;
import com.lry.task.TopNTaskStrategy;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.LongAdder;

public class Start {

    public static LongAdder successNum = new LongAdder();

    public static void main(String[] args) throws Exception {
//        bfs();//bfs组网

        System.out.println("---------------------------------------------------------------------------------------");

        dfs();//dfs组网
    }

    public static void bfs() throws Exception {

        List<Node> nodes = new ArrayList<>();
        for(int i =1;i<=100;i++){
            Node node = new Node("node"+i);
            nodes.add(node);
        }
        for (Node node : nodes) {
            node.registerNodes(nodes,node);
        }
        Node master = nodes.get(0);
        master.wakeup();

        for (Node node : nodes) {
            System.out.println(node);
        }

        execute(master);

    }

    public static void execute(Node master){
        List<Integer> list = new ArrayList<>();
        //提交任务，由上述构建好的节点一起完成该任务
        for(int i=0;i<9999999;i++){
            list.add(i);
        }
        TaskStrategy taskStrategy = new TopNTaskStrategy(3);
        Task task = new Task(list,taskStrategy);
        long start = System.currentTimeMillis();
        List<Integer> list1 = master.submitTask(task);
        System.out.println(System.currentTimeMillis()-start);
        System.out.println(list1);

        System.out.println("------------------------------");
        start = System.currentTimeMillis();
        System.out.println(taskStrategy.execute(list));
        System.out.println(System.currentTimeMillis()-start);
    }

    public static void dfs() throws Exception {
        List<Node> nodes = new ArrayList<>();
        for(int i =1;i<=10;i++){
            Node node = new Node("node"+i);
            nodes.add(node);
        }
        for (Node node : nodes) {
            node.registerNodes(nodes,node);
        }
        Node master = nodes.get(0);
        master.dfsWakeup();

        Thread.sleep(1000);

        for (Node node : nodes) {
            System.out.println(node);
        }

        execute(master);

    }
}
